// src/components/l-order-detail-goodsbtn/index.js
import { OrderModel } from '../../models/order'
const orderModel = new OrderModel()
import { encodeUnicode } from '../../utils/index.js'
import { GoodsModel } from '../../models/goods'
const goodsdetailModel = new GoodsModel()
const APP = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderinfo: Object,
    goodsinfo: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsModel: {},
    specNameList: [],
    skuVisible: false,
    activeIndex: null,
    cancelStatu: false,
    timestamp: new Date().getTime(),
    rangList: APP.globalData.cancelOrderReason
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //取消并退款
    cancel(event) {
      const self = this
      // wx.showModal({
      //   title: '提示',
      //   content: '是否取消该商品?',
      //   success: function(res) {
      //     if (res.confirm) {
      //       wx.showActionSheet({
      //         itemList: APP.globalData.cancelOrderReason,
      //         success(res) {
      //           // console.log(APP.globalData.cancelOrderReason[res.tapIndex])
      //             const params = {
      //               orderId: self.properties.orderinfo.id,
      //               orderGoodsId:self.properties.goodsinfo.id,
      //               returnReason: encodeUnicode(APP.globalData.cancelOrderReason[res.tapIndex])
      //             }
      //             orderModel.cancelGoods(params).then(res => {
      //               //替换提示语
      //               APP.WxService.showToast({ title: '取消成功', icon: 'none' })
      //               self.setData({
      //                 cancelStatu:true
      //               })
      //               wx.redirectTo({
      //                 url: '/pages/account/service/list/index'
      //               })
      //             })
      //         },
      //         fail(res) {
      //           console.log(res.errMsg)
      //         }
      //       })

      //     } else if (res.cancel) {

      //     }
      //   }
      // })
    },
    //关闭选择订单取消原因
    cancelPicker: function(e) {},

    //选择订单取消原因
    bindPickerChange: function(e) {
      const self = this
      wx.showModal({
        title: '提示',
        content: '是否取消该商品?',
        success: function(res) {
          if (res.confirm) {
            const params = {
              orderId: self.properties.orderinfo.id,
              orderGoodsId: self.properties.goodsinfo.id,
              returnReason: encodeUnicode(
                APP.globalData.cancelOrderReason[e.detail.value]
              )
            }
            orderModel.cancelGoods(params).then(res => {
              //替换提示语
              APP.WxService.showToast({
                title: '取消成功',
                icon: 'none'
              })
              self.setData({
                cancelStatu: true
              })
              wx.redirectTo({
                url: '/pages/account/service/list/index'
              })
            })
          } else if (res.cancel) {
          }
        }
      })
    },

    //查询尺码
    getGoodsSpec(e) {
      const self = this
      const params = {
        goodsId: self.properties.goodsinfo.goodsId
      }
      goodsdetailModel.getGoodSpec(params).then(res => {
        if (res.data.specNameList.length == 0) {
          APP.WxService.showToast({ title: '暂无尺码选择', icon: 'none' })
          return
        } else {
          self.setData({
            goodsModel: self.properties.goodsinfo,
            specNameList: res.data.specNameList,
            skuVisible: true
          })
        }
      })
    },

    //关闭弹出框
    closeSku() {
      this.setData({
        skuVisible: false
      })
    },

    //选中
    chooseSpec(options) {
      var that = this
      var index = parseInt(options.currentTarget.dataset.index)

      //设置当前样式
      that.setData({
        activeIndex: index
      })
    },

    //更换尺码
    action(event) {
      const self = this
      this.getGoodsSpec()
    },

    //确定
    submit(e) {
      if (this.data.activeIndex == null) {
        wx.showToast({
          title: '请选择尺码',
          icon: 'loading',
          duration: 500
        })
        return
      }
      const self = this
      const params = {
        orderId: self.properties.orderinfo.id,
        orderGoodsId: self.properties.goodsinfo.id,
        stockId: self.data.specNameList[self.data.activeIndex].id
      }
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面的对象
      orderModel
        .changeSize(params)
        .then(res => {
          // APP.WxService.redirectTo('/pages/account/order/detail/index?orderid='+params.orderId)
          APP.WxService.showToast({ title: '更换成功', icon: 'none' })
          self.setData({
            skuVisible: false
          })
          currentPage.getOrderDetail(params.orderId)
        })
        .catch()
    },

    //申请售后
    AfterSale(e) {
      const self = this
      const orderId = self.properties.orderinfo.id
      const orderGoodsId = self.properties.goodsinfo.id
      wx.redirectTo({
        url:
          '/pages/account/order/customer-service/index?orderId=' +
          orderId +
          '&orderGoodsId=' +
          orderGoodsId
      })
    },

    //添加评价--单个评价传1
    evaluation(e) {
      const self = this
      const orderId = self.properties.orderinfo.id
      const orderGoodsId = self.properties.goodsinfo.id
      const evaluateNum = 1
      wx.navigateTo({
        url:
          '/pages/account/order/comment/index?orderId=' +
          orderId +
          '&orderGoodsId=' +
          orderGoodsId +
          '&evaluateNum=' +
          evaluateNum
      })
    }
  }
})
